package com.controller;


import com.dao.BTypeDao;
import com.model.BType;
import com.model.Page;
import com.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TypeController {
    @Resource
    BTypeDao bTypeDao;
    @Resource
    Result result;

    private Result setResult(int status, String msg){
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    //获取用户列表
    @GetMapping(value = "/typeList")
    public String list(ModelMap model, Page page){
        List<BType> list=new ArrayList<>();
        List<BType> fTypeList=bTypeDao.getTypeByFid(0);
        for (BType bType:fTypeList){
            list.add(bType);
            List<BType> typeList=bTypeDao.getTypeByFid(bType.getId());
            list.addAll(typeList);
        }
        int total=bTypeDao.total(page);
        page.caculateLast(total);
        model.addAttribute("typeList",list);
        model.addAttribute("page",page);
        model.addAttribute("total",total);
        return "backend/type-list";
    }
    //添加分类页面
    @GetMapping(value = "/addTypeView")
    public String addTypeView(ModelMap model){
        List<BType> bTypeList=bTypeDao.getTypeByFid(0);
        model.addAttribute("bTypeList",bTypeList);
        return "backend/type-add";
    }
    //修改分类页面
    @GetMapping(value = "/updateTypeView")
    public String updateTypeView(ModelMap model, int id){
        List<BType> bTypeList=bTypeDao.getTypeByFid(0);
        BType bType=bTypeDao.getTypeById(id);
        model.addAttribute("bTypeList",bTypeList);
        model.addAttribute("bType",bType);
        return "backend/type-edit";
    }
    //修改分类
    @PostMapping(value = "/updateType")
    @ResponseBody
    public Result updateType(BType bType, ModelMap model){
        try {
            int row = bTypeDao.updateType(bType);
            if(row>0){
                return  this.setResult(1,"修改管理员成功！");
            }else {
                return  this.setResult(0,"修改管理员出错，请联系管理员！");
            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }
    //删除分类
    @PostMapping(value = "/delType")
    @ResponseBody
    public Result delType(Integer id, ModelMap model){
        try{
            BType bType=bTypeDao.getTypeById(id);
            if(bType==null){
                return this.setResult(0,"用户不存在！");
            }else {
                int row = bTypeDao.deleteType(id);
                if (row > 0) {
                    return this.setResult(1, "删除用户成功！");
                } else {
                    return this.setResult(0, "删除用户出错，请联系管理员！");
                }
            }
        }catch (Exception ex){
            return this.setResult(0,"删除用户出错，请联系管理员！");
        }
    }
    //添加商品信息
    @PostMapping(value = "/addType")
    @ResponseBody
    public Result addType(BType bType, ModelMap model){
        try{
            int row =bTypeDao.insertType(bType);
            if(row>0){
                return this.setResult(1,"添加商品成功！");
            }else {
                return this.setResult(2,"添加商品出错，请联系管理员！");
            }
        }catch (Exception ex){
            return  this.setResult(0,ex.getMessage());
        }
    }
}
